#include "postcard.h"
#include "ui_postcard.h"

#include "flowcontroler.h"
#include <QInputDialog>
#include <QtDebug>

PostCard::PostCard(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::PostCard)
{
    ui->setupUi(this);

    setStyleSheet("PostCard{border:1px solid blue;}");

    // 设置sizePolicy，让PostCard根据内容自动调整大小
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    adjustSize();
    connect(ui->label, &PixLabel::clicked, this, &PostCard::slot_pixLabel_Clicked);
}

PostCard::~PostCard()
{
    delete ui;
}

void PostCard::setPost(Core::Post &post)
{
    ui->labUser->setText(post.userName);
    ui->labTitle->setText(post.title);
    ui->editContent->setText(post.content);
    ui->labTime->setText(post.createdAt);


    // 设置 editContent 和 textEdit 的固定高度
    ui->editContent->setFixedHeight(75); // 设置高度为50
    ui->textEdit->setFixedHeight(150); // 设置高度为100

    //1///////////////////////////////////////////////////////////////////////////////////////////////////
    QPixmap pixmap = QPixmap(":/res/kongbai.png");
    ui->label->setPixmap(pixmap);
    //1///////////////////////////////////////////////////////////////////////////////////////////////////

    ui->textEdit->clear();
    ui->textEdit->setVisible(post.comments.size() > 0);
    for(auto p : post.comments)
    {
        QString content = QString("<b style='color:blue;'>%1</b>: %2")
                          .arg(p.userName)
                          .arg(p.content);
        ui->textEdit->append(content);
    }

    // 已经评论过的不显示评论
    bool hasComment = false;
    for(auto c : post.comments)
    {
        if(c.userId == FlowControler::Interface->getCurrentUser()->id)
        {
            hasComment = true;
            break;
        }
    }
//    ui->btnComment->setEnabled(!hasComment);

    // 已经点赞的不再能点赞
    bool hasLike = false;
    for(auto c : post.likes)
    {
        if(c.userId == FlowControler::Interface->getCurrentUser()->id)
        {
            hasLike = true;
            break;
        }
    }

    ui->btnLike->setEnabled(!hasLike);
    if(hasLike)
    {
        ui->btnLike->setText(QString("已赞(%1)").arg(post.likeCount));
    }
    else
    {
        ui->btnLike->setText(QString("点赞(%1)").arg(post.likeCount));
    }

    m_post = post;

    adjustSize();
}

Core::Post PostCard::getPost()
{
    return m_post;
}

void PostCard::setPixmapBuf(QByteArray buf)
{
    m_pixBuf = buf;
    QPixmap pixmap;
    QImage image;
    if(image.loadFromData(buf, "JPGE"))
    {
        QPixmap pixmap = QPixmap::fromImage(image);
        QSize labelSize = ui->label->size();
        QPixmap scaledPixmap = pixmap.scaled(labelSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        ui->label->setPixmap(scaledPixmap);
    }
}

void PostCard::on_btnLike_clicked()
{
    Core::LikePost post;
    post.userId = FlowControler::Interface->getCurrentUser()->id;
    post.postId = m_post.id;
    FlowControler::Interface->likePost(post);
}

void PostCard::on_btnComment_clicked()
{
    QString content = QInputDialog::getText(this, "输入", "请输入评论信息");
    if(content.isEmpty())
    {
        return;
    }
    Core::PostComment comment;
    comment.postId = m_post.id;
    comment.userId = FlowControler::Interface->getCurrentUser()->id;
    comment.content = content;
    FlowControler::Interface->postComment(comment);
}

void PostCard::slot_pixLabel_Clicked()
{
    QDialog *dialog = new QDialog(this);
    dialog->setWindowTitle("");
    dialog->resize(800, 600);

    QLabel *zoomedLabel = new QLabel(dialog);

    QImage image;
    if(image.loadFromData(m_pixBuf, "JPGE"))
    {
        QPixmap pixmap = QPixmap::fromImage(image);
        QSize labelSize = dialog->size();
        QPixmap scaledPixmap = pixmap.scaled(labelSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        zoomedLabel->setPixmap(scaledPixmap);
    }

    QVBoxLayout *layout = new QVBoxLayout(dialog);
    layout->addWidget(zoomedLabel);
    dialog->setLayout(layout);

    dialog->exec();
}
